package com.inspur.ourhis_afterend.Controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.inspur.ourhis_afterend.Dao.CostSetDao;
import com.inspur.ourhis_afterend.Dao.MemberDao;
import com.inspur.ourhis_afterend.Service.CostService;
import com.inspur.ourhis_afterend.Tools.PageAction;
import com.inspur.ourhis_afterend.Tools.Result;
import com.inspur.ourhis_afterend.model.CostSetTleDetails;
import com.inspur.ourhis_afterend.vo.CostSetDetailVo;
import com.inspur.ourhis_afterend.vo.CostVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
@ApiOperation("费用结算")
@RestController
@RequestMapping("/cost")
public class CostController {
    @Resource
    CostSetDao costSetDao;
    @Resource
    CostService costService;
    @Resource
    MemberDao memberDao;
    @ApiOperation("待结算列表条件查询")
    @ApiParam(value = "传入条件可空")
    @PostMapping("/selectBySelective")
    public Result selectBySelective(PageAction pageAction,
                                    @Param("memberid") Integer memberid,
                                    @Param("memberName") String memberName,
                                    @Param("roid") Integer roid){
        IPage<CostVo> page = new Page<CostVo>(pageAction.getPageNo(),pageAction.getPageSize());
        try {
            return Result.success(costService.selectBySelective(page,memberid,memberName,roid));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }
    @ApiOperation("结算处理添加费用结算明细表")
    @PostMapping("/addCostDetail")
    public Result addCostDetail(@RequestParam("memberid") String memberid, @Param("userid") Integer userid){
        try {
            CostVo costVo = costService.selectBySelective2(Integer.valueOf(memberid));
            System.out.println(" =========="+costVo);
            memberDao.increase(memberid,-costVo.getTotalPrice());
            CostSetTleDetails costSetTleDetails = new CostSetTleDetails();
            costSetTleDetails.setRoid(costVo.getRoid());
            costSetTleDetails.setMemberid(costVo.getMemberid());
            costSetTleDetails.setSettleamount(costVo.getTotalPrice());
            costSetTleDetails.setUserid(userid);
            int num = costService.insertSelective(costSetTleDetails);
            if (num >0){
            return Result.success("成功");
            }else {
            return Result.failure("失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure("失败"+e.toString());
        }
    }
    @ApiOperation("费用结算明细查询")
    @PostMapping("/selectCostDetail")
    public Result selectCostDetail(PageAction pageAction,
                                   @Param("memberid") Integer memberid,
                                   @Param("memberName") String memberName,
                                   @Param("roid") Integer roid){
        IPage<CostSetDetailVo> page = new Page<>(pageAction.getPageNo(),pageAction.getPageSize());
        try {
            return Result.success(costService.selectCostDetail(page, memberid, memberName, roid));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure("失败"+e.toString());
        }
    }
}
